import pinyin from 'pinyin'
import levenshtein from 'js-levenshtein'

export const retext = {
  components: {},
  data() {
    return {
      recorder: null, // 用来存储 RecorderMethods 实例
      commandMap: [], // 存储预处理后的命令列表
      rawCommandMap: [
        // 原始命令数据
        {
          taskid: 1,
          keyword: 'xuanzejianceshebeijimoti',
          isplay: false,
          taskdes: '切换菜单-选择检测设备及模体'
        },
        {
          taskid: 2,
          keyword: 'dakaishiyedeng',
          isplay: false,
          taskdes: '切换菜单-打开视野灯'
        },
        {
          taskid: 3,
          keyword: 'fangzhijianceshebeijimoti',
          isplay: false,
          taskdes: '切换菜单-放置检测设备及模体'
        },
        {
          taskid: 4,
          keyword: 'tiaojiesid',
          isplay: false,
          taskdes: '切换菜单-调节SID'
        },
        {
          taskid: 5,
          keyword: 'tiaojiezhaosheshiyan',
          isplay: false,
          taskdes: '切换菜单-调整照射野'
        },
        {
          taskid: 6,
          keyword: 'denglugongzuozhan',
          isplay: false,
          taskdes: '切换菜单-登录工作站'
        },
        {
          taskid: 7,
          keyword: 'shezhibaoguangcanshu',
          isplay: false,
          taskdes: '切换菜单-设置曝光参数'
        },
        {
          taskid: 8,
          keyword: 'puguang',
          isplay: false,
          taskdes: '切换菜单-曝光'
        },
        {
          taskid: 9,
          keyword: 'jilushuzhi',
          isplay: false,
          taskdes: '切换菜单-记录数值'
        },
        {
          taskid: 10,
          keyword: 'chakanmoxing',
          isplay: false,
          taskdes: '切换菜单-查看模型'
        },
        {
          taskid: 3,
          keyword: 'bofangtantoudonghua',
          isplay: true,
          taskdes: '播放动画'
        },
        {
          taskid: 10,
          keyword: 'fangda',
          isplay: false,
          taskdes: '模型放大'
        },
        {
          taskid: 10,
          keyword: 'suoxiao',
          isplay: false,
          taskdes: '模型缩小'
        },
        {
          taskid: 10,
          keyword: 'xiangzuoxuanzhuan',
          isplay: false,
          taskdes: '模型向左旋转'
        },
        {
          taskid: 10,
          keyword: 'xiangyouxuanzhuan',
          isplay: false,
          taskdes: '模型向右旋转'
        }
      ]
    }
  },
  methods: {
    // 打开录音设备
    recOpen() {
      this.$store.dispatch('wscont/wsStart')
      this.$recorder.recOpen()
    },

    // 结束录音
    recStop() {
      this.$recorder.recStop()
    },

    // 将文字转换为拼音
    getPinyinStr(text) {
      const cleanedText = text.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, '') // 去除标点符号等非文字字符
      return pinyin(cleanedText, {
        style: pinyin.STYLE_NORMAL,
        heteronym: false
      })
        .flat()
        .join('')
    },

    // 计算匹配度
    getMatchRatio(inputPinyin, keywordPinyin) {
      const distance = levenshtein(inputPinyin, keywordPinyin)
      return (keywordPinyin.length - distance) / keywordPinyin.length
    },

    // 预处理命令列表，将每个命令的拼音保存
    preprocessCommands() {
      this.commandMap = this.rawCommandMap.map((cmd) => ({
        ...cmd,
        keywordPinyin: this.getPinyinStr(cmd.keyword) // 将拼音提前处理
      }))
    },

    // 根据拼音匹配任务
    matchTaskByPinyin(inputText) {
      const inputPinyin = this.getPinyinStr(inputText)
      console.log('拼音转换：' + inputPinyin)
      let bestMatch = null
      let highestRatio = 0

      for (let cmd of this.commandMap) {
        const lenDiff = Math.abs(inputPinyin.length - cmd.keywordPinyin.length)
        if (lenDiff > 10) continue // 长度差异太大，跳过

        const ratio = this.getMatchRatio(inputPinyin, cmd.keywordPinyin)
        if (ratio > highestRatio) {
          highestRatio = ratio
          bestMatch = {
            taskid: cmd.taskid,
            isplay: cmd.isplay,
            keyword: cmd.keyword,
            taskdes: cmd.taskdes,
            matchRatio: ratio
          }
        }
      }

      if (bestMatch && highestRatio >= 0.65) {
        return bestMatch
      }

      return null
    }
  },

  created() {
    this.preprocessCommands() // 在组件创建时预处理命令
  },

  mounted() {
    // 如果需要，执行其他初始化
  }
}
